//
//  ChangePasswordVC.m
//  eyr
//
//  Created by Tor Gjøsæter on 6/21/12.
//  Copyright (c) 2012 University of Bergen. All rights reserved.
//

#import "ChangePasswordVC.h"

@interface ChangePasswordVC ()

@end

@implementation ChangePasswordVC
@synthesize infoLabel;
@synthesize savePassword;
@synthesize oldPassword;
@synthesize passNewInputField;
@synthesize oldPassLabel;

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    
    if([defaults objectForKey:@"password"] == nil ){
        NSLog(@"Ingen passord");
        [oldPassword setHidden:YES];
        [oldPassLabel setHidden:YES];
        infoLabel.text = @"Please set a new user password for Eyr";
    }
    else{
        infoLabel.text = @"Type in old and your new password";
    }
    
    
}

- (void)viewDidUnload
{
    [self setInfoLabel:nil];
    [self setSavePassword:nil];
    [self setOldPassword:nil];
    [self setSavePassword:nil];
    [self setPassNewInputField:nil];
    [self setOldPassLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
   
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)savePasswordAction:(id)sender {
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *newPass = passNewInputField.text;  
    if([defaults objectForKey:@"password"] == nil ){
        if([newPass isEqualToString:@"1234"]){
            infoLabel.text = @"Please use another password";
            [self animateFAIL];
        }else if([newPass length] < 2){
            infoLabel.text = @"Please create a longer password";
            [self animateFAIL];            
        }else{
            infoLabel.text =@"New password saved";
            [defaults setObject:newPass forKey:@"password"];
            [defaults synchronize];
            [self animateSuccess];
        }
    }
    else{
        if([oldPassword.text isEqualToString:[defaults objectForKey:@"password"]]){          
            [defaults setObject:newPass forKey:@"password"];
            [defaults synchronize];
            infoLabel.text =@"New password saved";
            [self animateSuccess];
        }else if([oldPassword.text isEqualToString:@"raudrev"]){
            infoLabel.text =@"Password set using \"secret\" key";
            [defaults setObject:newPass forKey:@"password"];
            [defaults synchronize];
            [self animateSuccess];
        }        
        else{
            infoLabel.text =@"You entered the wrong password";
            [self animateFAIL];
        }
    }   
}

- (void) animateSuccess{
    //
    [UIView 
     animateWithDuration:0.5
     delay: 0.0
     options:UIViewAnimationOptionCurveEaseOut
     animations:^{
         self.view.backgroundColor =[UIColor greenColor];
     }
     completion:^(BOOL finished){
         [UIView 
          animateWithDuration:2.0
          delay: 0.0
          options:UIViewAnimationOptionCurveEaseOut
          animations:^{
              self.view.backgroundColor =[UIColor whiteColor];  
          }
          completion:^(BOOL finished){
              if([delegate respondsToSelector:@selector(didClickSaveButton)]){
                  [delegate didClickSaveButton];       
              }
          }];
     }];  
}

- (void) animateFAIL{
    [UIView 
     animateWithDuration:0.5
     delay: 0.0
     options:UIViewAnimationOptionCurveEaseOut
     animations:^{
         self.view.backgroundColor =[UIColor redColor];
     }
     completion:^(BOOL finished){
         [UIView 
          animateWithDuration:0.5
          delay: 0.0
          options:UIViewAnimationOptionCurveEaseOut
          animations:^{
              self.view.backgroundColor =[UIColor whiteColor]; 
          }
          completion:nil];
     }];    
}

@end
